package com.example.proyyj.Controller;

import com.example.proyyj.Entity.Chinaware;
import com.example.proyyj.Entity.Relics;
import com.example.proyyj.Entity.ResultEntity;
import com.example.proyyj.Service.RelicsService;
import com.example.proyyj.Util.JSONTool;
import com.example.proyyj.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Relics/")
public class RelicsController {
    @Resource
    RelicsService relicsService;
    ResultEntity resultEntity=null;

    //删除文物
    @DeleteMapping("delRelics")
    private String delExhibitById (@RequestParam("id") int id){
        resultEntity =new ResultEntity();
        try {
            if (relicsService.delRelics(id)>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("删除成功");
            }else {
                resultEntity.setCode("1");
                resultEntity.setMsg("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误联系管理员");
        }
        return  JsonUtil.getJsonTool().toJson(resultEntity);
    }

    //查询文物
    @GetMapping("getRelics")
    private String getRelicsById(@RequestParam("id")int id){
        resultEntity=new ResultEntity();
        try{
            Relics relics=relicsService.getRelicsById(id);
            if(relics==null){
                resultEntity.setCode("1");
                resultEntity.setMsg("查无此人");
            }else {
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setData(relics);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg("系统错误，请联系管理员");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    //查询所有文物
    @GetMapping("getRelicList")
    private  String chinaware(){

        try {
            resultEntity=new ResultEntity();
            List<Relics> list=relicsService.getRelicsList();
            if (list!=null){
                resultEntity.setCode("0");
                resultEntity.setMsg("查询成功");
                resultEntity.setCount(list.size());
                resultEntity.setData(list);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("99999");
            resultEntity.setMsg("查询失败"+e.getMessage());
        }
        return  JSONTool.getJsonTool().toJson(resultEntity);
    }

}
